package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.HomeNewsDetailContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.CommentListBean;
import com.kmproject.haifu.mvp.model.entity.beans.HomeNewsBean;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsAddComment;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsGetCommentList;

import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 08/24/2019 13:46
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class HomeNewsDetailPresenter extends BasePresenter<HomeNewsDetailContract.Model, HomeNewsDetailContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public HomeNewsDetailPresenter(HomeNewsDetailContract.Model model, HomeNewsDetailContract.View rootView) {
        super(model, rootView);
    }


    /**
     * 首页资讯详情
     */
    public void getHomeNewsDetail(String headlineId) {
        mModel.getHomeNewsDetail(headlineId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<HomeNewsBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<HomeNewsBean> resultBean) {
                        mRootView.hideLoading();
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.getHomeNewsDetailSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 添加评论
     */
    public void addComment(ReqParamsAddComment paramsAddComment) {
        mModel.addComment(AppUtil.entityToMap(paramsAddComment)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        mRootView.hideLoading();
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.addCommentSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 取消赞
     */
    public void cancelComment(String commentId) {
        mModel.cancelComment(commentId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        mRootView.hideLoading();
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.cancelCommentSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * icon_un_like
     */
    public void likeComment(String commentId) {
        mModel.likeComment(commentId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        mRootView.hideLoading();
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.likeCommentSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 查询评论列表
     */
    public void getCommentList(ReqParamsGetCommentList reqParamsGetCommentList) {
        mModel.getCommentList(AppUtil.entityToMap(reqParamsGetCommentList)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<CommentListBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<CommentListBean>> resultBean) {
                        mRootView.hideLoading();
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.getCommentListSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                            mRootView.getCommentListFail();
                        }
                    }
                });
    }

    /**
     * 删除评论
     */
    public void deleteComment(String commentId) {
        mModel.deleteComment(commentId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        mRootView.hideLoading();
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.deleteCommentSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
